package com.hangwei.paperless.adapter;

import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.MainRepairActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.RecordListBean;
import com.hangwei.paperless.view.SealHelper;
import com.hangwei.paperless.widget.URLImageParser;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.utils.Tools;

import org.xml.sax.XMLReader;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/16.
 */

public abstract class FinalAdapter extends MyAdapter<RecordListBean, BaseViewHolder> {
    public void setEnableAll(boolean enableAll) {
        mEnableAll = enableAll;
    }

    private boolean mEnableAll = true;

    public FinalAdapter(@Nullable List<RecordListBean> data) {
        super(R.layout.item_final_check, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final RecordListBean item) {
        String source = Tools.checkedString(item.description);
//        String all = source.replaceAll("<p>", "<p  style=\"display:block;margin:0 0;\">");
        //替换掉P标签开头
        String all = source.replaceAll("<p>", "").replaceAll("<p .?>", "");
        while (all.endsWith("</p>")) {
            all = all.substring(0, all.lastIndexOf("</p>"));
        }
        while (all.endsWith("<br/>")) {
            all = all.substring(0, all.lastIndexOf("<br/>"));
        }
        all = all.replaceAll("</p>", "<br/>");
        Spanned html;
        TextView tvDes = helper.getView(R.id.tvDes);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
            html = Html.fromHtml(all, Html.FROM_HTML_MODE_COMPACT, new URLImageParser(tvDes, mContext), new Html.TagHandler() {
                @Override
                public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {

                }
            });
        else
            html = Html.fromHtml(all, new URLImageParser(tvDes, mContext), new Html.TagHandler() {
                @Override
                public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {

                }
            });
        helper.setText(R.id.tvItem, Tools.checkedString(item.item))
                .setText(R.id.tvSeq, Tools.checkedString(item.seq))
                .setText(R.id.tvDes, html);

        final boolean able = mEnableAll && !TextUtils.isEmpty(MainRepairActivity.mStepId) && MainRepairActivity.mStepId.equals(item.id);
        RelativeLayout flInspector = helper.getView(R.id.flInspector);
        flInspector.removeAllViews();
        SealHelper sealHelper1 = new SealHelper(mContext) {
            @Override
            protected void onSeal() {
                String resultVal = null;
                seal(item, 2, resultVal);
            }
        };
        sealHelper1.bindData(new SealHelper.SealBean(item.isInspectorNA,item.inspectorStampId, item.inspectorId, item.inspectorStampTypeCode, item.inspectorStampCode, item.inspectorDate,item.inspector, able && !TextUtils.isEmpty(item.technicianId)));
        flInspector.addView(sealHelper1.getItemView());
//        flInspector.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (able && !TextUtils.isEmpty(item.technicianId)) {
//                    String resultVal = null;
//                    seal(item, 2, resultVal);
//                }
//            }
//        });
    }

    protected abstract void seal(RecordListBean item, int stampRole, String resultVal);
}
